/**
 * Copyright (C) © 2014 深圳市掌玩网络技术有限公司
 */


import java.io.IOException;
import java.net.Socket;
import java.util.Vector;

/**
 * <p>
 * <strong>Features draft description.主要功能介绍</strong>
 * </p>
 * 
 * @author Administrator
 * @Create at 2015-11-26 上午9:54:33
 * @Version 1.0
 * 
 */
public class TcpTranser {
     Vector<Socket> clientList = new Vector<Socket>();

    public TcpTranser() {
       
    }
    int packetCuntInSecond = 0;
    /**
     * 
     */
    public void startAll() {
        // listen read
        for (Socket socket : clientList) {
            tranAsync(socket);
        }

    }
    public void addSocket(Socket socket) {
        clientList.add(socket);
        tranAsync(socket);
        System.out.println(">>>> User Join:->"+
                socket.getInetAddress().getHostAddress() + ":"
                + socket.getPort());
    }

    private void tranAsync(final Socket socket) {
        new Thread() {
            public void run() {
                byte[] bytes = new byte[1024];
                boolean isRunning = socket.isConnected();
                while (isRunning) {
                    int read;
                    try {
                        read = IOUtil.read(socket.getInputStream(), bytes);
                    } catch (IOException e) {
                        e.printStackTrace();
                        isRunning = false;
                        removeSocket(socket);
                        break;
                    }
                    if (bytes[12]==100) {
                        System.out.println("recv ping");
                        try {
                            IOUtil.send(socket.getOutputStream(), bytes, 0, read);
                        } catch (IOException e) {
                            e.printStackTrace();
                            removeSocket(socket);
                        }
                        continue;
                    }
                    if (read>0) {
                        for (int i = 0; i < clientList.size(); i++) {
                            final Socket socket2 = clientList.get(i);
                            if (socket2 != socket) {

                                try {
                                    IOUtil.send(socket2.getOutputStream(), bytes, 0, read);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                    
                                }
                            }
                        }
                    }
                }
            }

           
        }.start();

    }
    private void removeSocket(final Socket socket) {
        if (clientList.contains(socket)) {
            boolean isRemove =clientList.remove(socket);
            if (!isRemove) {
                System.out.println("reomve fail:->"+
                        socket.getInetAddress().getHostAddress() + ":"
                        + socket.getPort());
                return;
            }
            System.out.println("<<<< User Leave:->"+
                    socket.getInetAddress().getHostAddress() + ":"
                    + socket.getPort());
            System.out.println("Current User count:"+clientList.size());
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    /**
     * 
     */
    protected void closeAll() {
        for (int i = 0; i < clientList.size(); i++) {
            final Socket socket2 = clientList.get(i);
            try {
                socket2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        clientList.removeAll(clientList);
        
    }
}
